package com.yanyi.wechat.handler;

import com.yanyi.wechat.builder.TextBuilder;
import com.yanyi.wechat.utils.JsonUtils;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutNewsMessage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.text.MessageFormat;
import java.util.Map;

import static com.sun.xml.internal.ws.spi.db.BindingContextFactory.LOGGER;
import static me.chanjar.weixin.common.api.WxConsts.XmlMsgType;

/**
 * @author Binary Wang(https://github.com/binarywang)
 */
@Component
public class MsgHandler extends AbstractHandler {

    @Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,
                                    Map<String, Object> context, WxMpService wxMpService,
                                    WxSessionManager sessionManager) {

        if (!wxMessage.getMsgType().equals(XmlMsgType.EVENT)) {
            //TODO 可以选择将消息保存到本地
        }

        //当用户输入关键词如“你好”，“客服”等，并且有客服在线时，把消息转发给在线客服
        try {
            if (StringUtils.startsWithAny(wxMessage.getContent(), "你好", "客服")
                    && wxMpService.getKefuService().kfOnlineList()
                    .getKfOnlineList().size() > 0) {
                return WxMpXmlOutMessage.TRANSFER_CUSTOMER_SERVICE()
                        .fromUser(wxMessage.getToUser())
                        .toUser(wxMessage.getFromUser()).build();
            }
        } catch (WxErrorException e) {
            e.printStackTrace();
        }
        //TODO 组装回复消息
        switch (wxMessage.getContent()) {
            case "签到":
                return sendInSignMessage(wxMessage, context, wxMpService, sessionManager);
            case "猜拳":
                return sendMessage(wxMessage, context, wxMpService, sessionManager);
            default:
                String content = "收到信息内容：" + JsonUtils.toJson(wxMessage);
                return new TextBuilder().build(content, wxMessage, wxMpService);
        }
    }

    private WxMpXmlOutMessage sendMessage(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService wxMpService,
                                          WxSessionManager sessionManager) {
        String content = "进入猜拳模式";
        return new TextBuilder().build(content, wxMessage, wxMpService);
    }

    private WxMpXmlOutNewsMessage sendInSignMessage(WxMpXmlMessage wxMessage,
                                                    Map<String, Object> context, WxMpService wxMpService,
                                                    WxSessionManager sessionManager) {
        String signUrl = "http://vcard.zoesoft.com.cn/weiya/view/form.html";
        String url = MessageFormat.format(signUrl, wxMessage.getFromUser());
        LOGGER.info("签到url:" + signUrl);
        WxMpXmlOutNewsMessage.Item item = new WxMpXmlOutNewsMessage.Item();
        item.setDescription("请点击进去智业尾牙年会签到页面");
        item.setPicUrl("https://mmbiz.qpic.cn/mmbiz_jpg/B0md6NdhhMRguia0l7AUGZ1mRUzm3ibv9fVqiblSON5VyS6ceAjWLZHGJQ9CnbeUKOOg1xkvQQB4QprfdkLmA9gicw/0?wx_fmt=jpeg");
        item.setTitle("签到");
        item.setUrl(url);

        WxMpXmlOutNewsMessage m = WxMpXmlOutMessage.NEWS()
                .fromUser(wxMessage.getToUser())
                .toUser(wxMessage.getFromUser())
                .addArticle(item)
                .build();
        return m;
    }

}
